package com.wangyadong.hobby.schedule.jobtask.dsa.binaryTree;

import java.util.List;

public abstract class AbtractTree<E> implements Tree<E> {

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public boolean isInternal(Position position) throws IllegalArgumentException {
        return numChildren(position) == 0;
    }

    @Override
    public boolean isExternal(Position position) throws IllegalArgumentException {
        return numChildren(position) > 0;
    }

    /***
     * 前序遍历
     * @param p
     * @param snapshot
     */
    public void preorderSubtree(Position<E> p, List<Position<E>> snapshot) {
        snapshot.add(p); // for preorder, we add position p before exploring subtrees
        for (Position<E> c : children(p))
            preorderSubtree(c, snapshot);
    }


    public void postOrderSubTree(Position<E> p, List<Position<E>> snapshot) {
        for (Position<E> c : children(p))
            preorderSubtree(c, snapshot);
        snapshot.add(p);
    }


}

